package com.youboy.wechatapi.utils.wechat;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ProjectName：wechatapi
 * @author：冉尧（Yvan）
 * @CreateTime：2014年10月30日 上午9:29:41
 * @Remarks：
 */
public class YouboyApi {

	//主站 1
	public final static String WWW_INDEX = "http://www\\.youboy\\.com/?";
	//m站点 2
	public final static String M_INDEX = "http://m\\.youboy\\.com";
	//m站点产品列表 3
	public final static String M_PRODUCT_LIST = "http://m\\.youboy\\.com/p\\?q=.+";
	//m站点产品详情 4
	public final static String M_PRODUCT_INFO = "http://m\\.youboy\\.com/p\\d+.html";
	//m站点公司列表 5
	public final static String M_COMPANY_LIST = "http://m\\.youboy\\.com/c\\?q=.+";
	//m站点公司详情 6
	public final static String M_COMPANY_INFO = "http://m\\.youboy\\.com/c\\d+.html";
	//微商铺 7
	public final static String WECHAT_SHOP = "http://wx\\.youboy\\.com/3g\\.php.+";
	//VIP展位 8
	public final static String VIP_SHOW = "http://wechatapi\\.youboy\\.com/vip/show.*";
	
	public static Map<Integer, String> apiMap = new HashMap<Integer, String>();
	static {
		apiMap.put(1, WWW_INDEX);
		apiMap.put(2, M_INDEX);
		apiMap.put(3, M_PRODUCT_LIST);
		apiMap.put(4, M_PRODUCT_INFO);
		apiMap.put(5, M_COMPANY_LIST);
		apiMap.put(6, M_COMPANY_INFO);
		apiMap.put(7, WECHAT_SHOP);
		apiMap.put(8, VIP_SHOW);
	}
	
	/**
	 * 根据链接获取访问类型
	 * @param url
	 * @return
	 */
	public static Integer getVisitType(String url) throws Exception{
		
		boolean result = false;
		Integer type = null;
		
		Pattern pattern = null;
		Matcher matcher = null;
		for (Integer key : apiMap.keySet()) {
			String value = apiMap.get(key);
			if(key > 100 && value.equals(url)){
				type = key;
				return type;
			}
			pattern = Pattern.compile(value);
			matcher = pattern.matcher(url);
			result = matcher.matches();
			if(result){
				type = key;
				return type;
			}
		}
		return type;
	}

}
